package com.ruix.app.ui.usercenter;



import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.ruix.app.R;
import com.ruix.app.mapper.PurseRecordMapper;
import com.ruix.app.model.PurseRecord;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.ui.adapter.PurseRecordAdapter;
import com.ruix.app.ui.base.BaseActivity;
import java.util.ArrayList;

import io.reactivex.disposables.Disposable;

/**
 * Created by hadexs on 2017/5/4.
 */

public class PurseDetailActivity extends BaseActivity {

    @Override
    protected int loadViewLayout() {
        return R.layout.activity_purse_detail;
    }

    private PurseRecordAdapter adapter;
    private ListView messageList;
    private SpringView springView;
    private SwipeRefreshLayout mRefreshLayout;
    private int LOAD_COUNT = 20;

    @Override
    protected void initView() {
        setTitle("交易明细");

//        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
//        mRefreshLayout.setColorSchemeResources(R.color.bg_nav_default
//                , R.color.bg_nav_yellow
//                , R.color.bg_default
//                , R.color.text_color_pink);

        messageList = (ListView) findViewById(R.id.message_list);
        adapter = new PurseRecordAdapter(this);
        messageList.setAdapter(adapter);
        springView = (SpringView) findViewById(R.id.spring_view);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setGive(SpringView.Give.BOTH);
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));

    }

    @Override
    protected void initLocalData() {
        loadFundDetail(LOAD_COUNT, 0, true);
    }

    @Override
    protected void initListener() {
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                loadFundDetail(LOAD_COUNT, 0, false);
            }

            @Override
            public void onLoadmore() {
                loadFundDetail(LOAD_COUNT, adapter.getCount(), false);
            }
        });
    }

    private void loadFundDetail(int limit, int offset, final boolean loading){
        if (loading){
            showLoading();
        }
        RequestFactory.fundDetail(limit, offset, new PurseRecordMapper(), new RXObserver<ArrayList<PurseRecord>>() {

            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ArrayList<PurseRecord> value) {
                if (value != null) {
                    adapter.addAll(value);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (loading){
                    dismissLoading();
                }
                resolveException(e);
            }

            @Override
            public void onComplete() {
                if (loading){
                    dismissLoading();
                }
            }
        });

    }

}
